package com.chuang.bootplus.controller.admin;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chuang.bootplus.base.utils.RedisUtil;
import com.chuang.bootplus.controller.admin.po.PagePO;
import com.chuang.bootplus.controller.admin.vo.RecordVO;
import com.chuang.bootplus.entity.document.Document;
import com.chuang.bootplus.entity.topic.Topic;
import com.chuang.bootplus.entity.model.UploadRecord;
import com.chuang.bootplus.entity.word.Word;
import com.chuang.bootplus.service.DocumentService;
import com.chuang.bootplus.service.TopicService;
import com.chuang.bootplus.service.UploadRecordService;
import com.chuang.bootplus.service.WordService;
import io.swagger.annotations.Api;
import com.chuang.bootplus.base.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = {"后台WTD数据管理"})
@RestController
@RequestMapping("/admin")
public class WTDController {

    @Autowired
    RedisUtil redisUtil;


    @Resource
    UploadRecordService uploadRecordService;

    @Autowired
    WordService wordService;

    @Autowired
    DocumentService documentService;

    @Autowired
    TopicService topicService;

    @PostMapping("/getList")
    public ApiResponse<RecordVO> getList(@RequestBody PagePO po)  {
        //SELECT SUM(`doc_num`) AS `dnum`,SUM(`word_num`) AS `wnum`,SUM(`topic_num`) AS `tnum`,`upload_time` FROM `upload_record`  GROUP BY `upload_time` LIMIT 2,1
        QueryWrapper<UploadRecord> wrapper = new QueryWrapper<>();
        wrapper.select("sum(doc_num) AS add_doc,sum(word_num) AS add_word,sum(topic_num) AS add_topic,upload_time").groupBy("upload_time");
        List<Map<String, Object>> getAll = uploadRecordService.listMaps(wrapper);
        Integer allTotal=getAll.size();
        wrapper.last("LIMIT "+(po.getPageNumber()-1)+","+po.getPageSize());
        // 获取到新增的个数
        //[{upload_time=2020-11-12, tnum=8, wnum=18, dnum=8}, {upload_time=2020-11-11, tnum=9, wnum=17, dnum=12}]
        List<Map<String, Object>> maps = uploadRecordService.listMaps(wrapper);
        RecordVO recordVO = new RecordVO();
        ArrayList<RecordVO> recordVOS = new ArrayList<>();
        maps.forEach(t->{
            RecordVO recordVO2 = JSON.parseObject(JSON.toJSONString(t), RecordVO.class);
            recordVO2.setWord(wordService.count(new LambdaQueryWrapper<Word>().like(Word::getGmtCreate,t.get("upload_time"))));
            recordVO2.setTopic(topicService.count(new LambdaQueryWrapper<Topic>().like(Topic::getGmtCreate,t.get("upload_time"))));
            recordVO2.setDoc(documentService.count(new LambdaQueryWrapper<Document>().like(Document::getGmtCreate,t.get("upload_time"))));
            recordVOS.add(recordVO2);
            System.out.println(recordVO2);
        });
        System.out.println(recordVOS);
        return new ApiResponse(recordVOS,recordVOS.size(),allTotal);
    }


}
